//
//  ForecastControllerViewController.m
//  BiuWeather
//
//  Created by 楼楚 on 16/3/11.
//  Copyright © 2016年 Chu Lou. All rights reserved.
//

#import "ForecastControllerViewController.h"
#import "WeatherNumberMeaningTransform.h"

@interface ForecastControllerViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView            *tableView;
@property (nonatomic, strong) UITableViewCell      *tableViewCell;
@property (nonatomic, strong) UILabel                    *weekDayInfoLabel;
@property (nonatomic, strong) UILabel                    *dateLabel;
@property (nonatomic, strong) UILabel                    *minTempLabel;
@property (nonatomic, strong) UILabel                    *maxTempLabel;
@property (nonatomic, strong) UILabel                    *weatherLabel;
@property (nonatomic, strong) UILabel                    *weatherInfoLabel;
@end

@implementation ForecastControllerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initTableView];
    // Do any additional setup after loading the view.
}

-(void)initTableView{
    
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator   = NO;
    self.tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 7;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];
    //    如果如果没有多余单元，则需要创建新的单元
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:TableSampleIdentifier];
    }
    
    else {
        while ([cell.contentView.subviews lastObject ]!=nil) {
            [(UIView*)[cell.contentView.subviews lastObject]removeFromSuperview];
        }
    }
    NSUInteger row = [indexPath row];
    NSDictionary *todayInfo =self.list[row];
    NSDictionary *dic1 = [todayInfo objectForKey:@"tmp"];
    NSDictionary *dic2 = [todayInfo objectForKey:@"cond"];
    NSString *string = [todayInfo objectForKey:@"date"];
    NSString *newStr = [string stringByReplacingOccurrencesOfString:@"-" withString:@""];
    NSDateFormatter *inputFormatter =  [[NSDateFormatter alloc]init];
    [inputFormatter setDateFormat:@"yyyyMMdd"];
    NSDate *inputDate = [inputFormatter dateFromString:newStr];
    NSTimeZone *fromzone = [NSTimeZone systemTimeZone];
    NSInteger frominterval = [fromzone secondsFromGMTForDate: inputDate];
    NSDate *fromDate = [inputDate  dateByAddingTimeInterval: frominterval];
    NSDateFormatter *new = [[NSDateFormatter alloc]init];
    [new setDateFormat:@"EEE"];
    NSString *weekStr = [new stringFromDate:fromDate];
    
    // 星期几
    if (iPhone4_4s || iPhone5_5s) {
        
        self.weekDayInfoLabel               = [[UILabel alloc] initWithFrame:CGRectMake(17 + 10, 24, 100, 60)];
        self.weekDayInfoLabel.textAlignment = NSTextAlignmentLeft;
        self.weekDayInfoLabel.font          = [UIFont fontWithName:LATO_THIN size:28.f];
        self.weekDayInfoLabel.text          = weekStr;
        
    } else if (iPhone6_6s) {
        
        self.weekDayInfoLabel               = [[UILabel alloc] initWithFrame:CGRectMake(17 + 10, 24, 100, 60)];
        self.weekDayInfoLabel.textAlignment = NSTextAlignmentLeft;
        self.weekDayInfoLabel.font          = [UIFont fontWithName:LATO_THIN size:34.f];
        self.weekDayInfoLabel.text          = weekStr;
        
    } else if (iPhone6_6sPlus) {
        
        self.weekDayInfoLabel               = [[UILabel alloc] initWithFrame:CGRectMake(17 + 10, 24, 100, 60)];
        self.weekDayInfoLabel.textAlignment = NSTextAlignmentLeft;
        self.weekDayInfoLabel.font          = [UIFont fontWithName:LATO_THIN size:40.f];
        self.weekDayInfoLabel.text          = weekStr;
        
    } else {
        
        self.weekDayInfoLabel               = [[UILabel alloc] initWithFrame:CGRectMake(17 + 10, 24, 100, 60)];
        self.weekDayInfoLabel.textAlignment = NSTextAlignmentLeft;
        self.weekDayInfoLabel.font          = [UIFont fontWithName:LATO_THIN size:28.f];
        self.weekDayInfoLabel.text          = weekStr;
    }
    
    [self.weekDayInfoLabel sizeToFit];
    [cell.contentView addSubview:self.weekDayInfoLabel];
    
    // 日期
    self.dateLabel               = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 12.f)];
    self.dateLabel.textAlignment = NSTextAlignmentCenter;
    self.dateLabel.font          = [UIFont fontWithName:LATO_REGULAR size:8.f];
    self.dateLabel.text          = @"2015.2.25";
    self.dateLabel.textColor     = [UIColor whiteColor];
    
    UIView *dateBlackView         = [[UIView alloc] initWithFrame:CGRectMake(Width - 50, 0, 50, 12.f)];
    dateBlackView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.95];
    [dateBlackView addSubview:self.dateLabel];
    
    // 最低温度
    if (iPhone4_4s || iPhone5_5s) {
        
        self.minTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100, 25, 200, 70)];
        self.minTempLabel.textAlignment = NSTextAlignmentLeft;
        self.minTempLabel.font          = [UIFont fontWithName:LATO_THIN size:28];
//        self.minTempLabel.text          = @"-200.0°";
        self.minTempLabel.text = [NSString stringWithFormat:@"%@°",[dic1 objectForKey:@"min"]];
    } else if (iPhone6_6s) {
        
        self.minTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(110, 25, 200, 70)];
        self.minTempLabel.textAlignment = NSTextAlignmentLeft;
        self.minTempLabel.font          = [UIFont fontWithName:LATO_THIN size:34];
        self.minTempLabel.text = [NSString stringWithFormat:@"%@°",[dic1 objectForKey:@"min"]];
        
    } else if (iPhone6_6sPlus) {
        
        self.minTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(120, 25, 200, 70)];
        self.minTempLabel.textAlignment = NSTextAlignmentLeft;
        self.minTempLabel.font          = [UIFont fontWithName:LATO_THIN size:40];
     self.minTempLabel.text = [NSString stringWithFormat:@"%@°",[dic1 objectForKey:@"min"]];
    } else {
        
        self.minTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100, 25, 200, 70)];
        self.minTempLabel.textAlignment = NSTextAlignmentLeft;
        self.minTempLabel.font          = [UIFont fontWithName:LATO_THIN size:28];
        self.minTempLabel.text = [NSString stringWithFormat:@"%@°",[dic1 objectForKey:@"min"]];    }
    
    [self.minTempLabel sizeToFit];
    [cell.contentView addSubview:self.minTempLabel];
   
    
    // 最高温度
    if (iPhone4_4s || iPhone5_5s) {
        
        self.maxTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100 + 80, 25, 200, 70)];
        self.maxTempLabel.textAlignment = NSTextAlignmentLeft;
        self.maxTempLabel.font          = [UIFont fontWithName:LATO_THIN size:28];
//        self.maxTempLabel.text          = @"-200.0°";
        self.maxTempLabel.text = [NSString stringWithFormat:@"%@°",[dic1 objectForKey:@"max"]];
    } else if (iPhone6_6s) {
        
        self.maxTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100 + 80 + 20, 25, 200, 70)];
        self.maxTempLabel.textAlignment = NSTextAlignmentLeft;
        self.maxTempLabel.font          = [UIFont fontWithName:LATO_THIN size:34];
        self.maxTempLabel.text = [NSString stringWithFormat:@"%@°",[dic1 objectForKey:@"max"]];
    } else if (iPhone6_6sPlus) {
        
        self.maxTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100 + 80 + 40, 25, 200, 70)];
        self.maxTempLabel.textAlignment = NSTextAlignmentLeft;
        self.maxTempLabel.font          = [UIFont fontWithName:LATO_THIN size:40];
        self.maxTempLabel.text = [NSString stringWithFormat:@"%@°",[dic1 objectForKey:@"max"]];
        
    } else {
        
        self.maxTempLabel               = [[UILabel alloc] initWithFrame:CGRectMake(100 + 80, 25, 200, 70)];
        self.maxTempLabel.textAlignment = NSTextAlignmentLeft;
        self.maxTempLabel.font          = [UIFont fontWithName:LATO_THIN size:28];
        self.maxTempLabel.text = [NSString stringWithFormat:@"%@°",[dic1 objectForKey:@"max"]];    }
    
    [self.maxTempLabel sizeToFit];
    [cell.contentView addSubview:self.maxTempLabel];
   
    
    // 天气图标
    if (iPhone4_4s || iPhone5_5s) {
        
        self.weatherLabel               = [[UILabel alloc] initWithFrame:CGRectMake(250, 15, 60, 60)];
        self.weatherLabel.textAlignment = NSTextAlignmentCenter;
        self.weatherLabel.font          = [UIFont fontWithName:WEATHER_TIME size:40.f];
        self.weatherLabel.text = [WeatherNumberMeaningTransform fontTextWeatherNumber:[dic2 objectForKey:@"code_d"]];
    } else if (iPhone6_6s) {
        
        self.weatherLabel               = [[UILabel alloc] initWithFrame:CGRectMake(250 + 40, 20, 60, 60)];
        self.weatherLabel.textAlignment = NSTextAlignmentCenter;
        self.weatherLabel.font          = [UIFont fontWithName:WEATHER_TIME size:45.f];
        self.weatherLabel.text = [WeatherNumberMeaningTransform fontTextWeatherNumber:[dic2 objectForKey:@"code_d"]];
        
    } else if (iPhone6_6sPlus) {
        
        self.weatherLabel               = [[UILabel alloc] initWithFrame:CGRectMake(250 + 70, 24, 60, 60)];
        self.weatherLabel.textAlignment = NSTextAlignmentCenter;
        self.weatherLabel.font          = [UIFont fontWithName:WEATHER_TIME size:50.f];
      self.weatherLabel.text = [WeatherNumberMeaningTransform fontTextWeatherNumber:[dic2 objectForKey:@"code_d"]];
        
    } else {
        
        self.weatherLabel               = [[UILabel alloc] initWithFrame:CGRectMake(250, 15, 60, 60)];
        self.weatherLabel.textAlignment = NSTextAlignmentCenter;
        self.weatherLabel.font          = [UIFont fontWithName:WEATHER_TIME size:40.f];
        self.weatherLabel.text = [WeatherNumberMeaningTransform fontTextWeatherNumber:[dic2 objectForKey:@"code_d"]];
    }
    [cell.contentView addSubview:self.weatherLabel];
    
    
    // 天气描述信息
    self.weatherInfoLabel               = [[UILabel alloc] initWithFrame:CGRectMake(5, 15, 300, 12)];
    self.weatherInfoLabel.textAlignment = NSTextAlignmentLeft;
    self.weatherInfoLabel.font          = [UIFont fontWithName:LATO_REGULAR size:10];
    self.weatherInfoLabel.text = [dic2 objectForKey:@"txt_d"];
    [cell.contentView addSubview:self.weatherInfoLabel];
        // 线条
    UIView *line         = [[UIView alloc] initWithFrame:CGRectMake(0, Width / 4.f - 0.5, Width, 0.5)];
    line.backgroundColor = [UIColor blackColor];
    line.alpha           = 0.1;
    [cell.contentView addSubview:line];
   
    return cell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return Width/4.f;
}


- (id)initWithArray:(NSArray *)forecastlist{
    self = [super self];
    if (self) {
        self.list = forecastlist;
    }
    return self;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (scrollView.contentOffset.y <= -60) {
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
        
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
